package com.morningstar.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.dao.entity.SysConfig;
import com.morningstar.system.dao.mapper.SysConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * 参数配置 服务层实现
 *
 * @author ruoyi
 */
@Slf4j
@Service
public class SysConfigService extends ServiceImpl<SysConfigMapper, SysConfig> {

    /**
     * 添加或修改参数配置
     *
     * @param sysConfig 系统配置实体类
     * @return 校验结果
     */
    public ServiceCheckResult checkAddAndUpdate(SysConfig sysConfig) {
        LambdaQueryWrapper<SysConfig> checkConfigNameWrapper = new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigName, sysConfig.getConfigName())
                .ne(sysConfig.getConfigId() != null, SysConfig::getConfigId, sysConfig.getConfigId());
        if (this.count(checkConfigNameWrapper) > 0) {
            return new ServiceCheckResult(true, "配置名称已存在");
        }
        LambdaQueryWrapper<SysConfig> checkConfigKeyWrapper = new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigKey, sysConfig.getConfigKey())
                .ne(sysConfig.getConfigId() != null, SysConfig::getConfigId, sysConfig.getConfigId());
        if (this.count(checkConfigKeyWrapper) > 0) {
            return new ServiceCheckResult(true, "配置KEY已存在");
        }
        return new ServiceCheckResult(false);
    }

}
